home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0005_PERM-STR.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  57 lines

  1. {
  2.  
  3. Here it is.  note that this permutes a set of Characters.  if you want to
  4. do something different, you will have to modify the code, but that should
  5. be easy.
  6.  
  7. }
  8.  
  9. Type
  10.   tThingRec = Record
  11.     ch  : Char;
  12.     occ : Boolean;
  13.   end;
  14.  
  15. Var
  16.   Thing       : Array[1..255] of tThingRec;
  17.   EntryString : String;
  18.  
  19. Procedure Permute(num : Byte);
  20. { N.B.  Procedure _must_ be called With num = 1;
  21.   it then calls itself recursively,
  22.   incrementing num }
  23. Var
  24.   i : Byte;
  25. begin
  26.   if num > length(EntryString) then
  27.   begin
  28.     num := 1;
  29.     For i := 1 to length(EntryString) do
  30.       Write(Thing[i].Ch);                 { You'll want to direct }
  31.     Writeln;                              { output somewhere else }
  32.   end
  33.   else
  34.   begin
  35.     For i := 1 to length(EntryString) do
  36.     begin
  37.       if (not Thing[i].Occ) then
  38.       begin
  39.         Thing[i].Occ := True;
  40.         Thing[i].Ch := EntryString[num];
  41.         Permute(succ(num));
  42.         Thing[i].Occ := False;
  43.       end;
  44.     end;
  45.   end;
  46. end;
  47.  
  48.  
  49. begin
  50.   FillChar(Thing,sizeof(Thing),0);
  51.   Write('Enter String of Characters to Permute: ');
  52.   Readln(EntryString);
  53.   Permute(1);
  54.   Writeln;
  55.   Writeln('Done');
  56. end.
  57.